<style lang="less">
</style>
<template>
    <div class="main">
        <div class="main-header">
            <el-breadcrumb>
                <el-breadcrumb-item :to="{ path: '/categories' }">分类管理</el-breadcrumb-item>
                <el-breadcrumb-item>{{itemModel.id ? '修改分类' : '添加分类'}}</el-breadcrumb-item>
            </el-breadcrumb>
        </div>

        <el-form class="main-form" label-width="100px" :model="itemModel" @submit.native.prevent="save()">
            <el-form-item label="父级分类" v-if="itemModel.parent_id">
               <el-input v-model="itemModel.parent.name" disabled/>
            </el-form-item>
            <el-form-item label="名字">
                <el-input v-model="itemModel.name"/>
            </el-form-item>
            <el-form-item label="排序">
                <el-input-number v-model="itemModel.priority" :min="0"/>
            </el-form-item>

            <el-form-item>
                <el-button native-type="submit" type="primary" :loading="!itemModel.$resolved">保存</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
// import Promise from 'bluebird';
import CategoryModel from '../models/category';
import ItemController from '../controllers/item';

export default new ItemController({
    data() {
        return {
            itemModel: new CategoryModel({
                parent_id: 0,
                name: '',
                priority: 1,
                status: CategoryModel.ONLINE,
            })
        };
    },
    methods: {
        afterLoadModel() {
        },
        beforeSave() {
            if(+this.$route.query.parent_id > 0) {
                return CategoryModel.get({
                    id: this.$route.query.parent_id,
                })
                .$promise
                .then(parent => {
                    this.itemModel.parent_id = parent.id;
                });
            }
        }
    }
});
</script>
